/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUIComponents;

import MainClasses.Event.Event;
import MainClasses.Event.UserCheckEvent;
import MainClasses.Event.UserEvent;
import MainClasses.MyDate;
import MainClasses.main;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
 *
 * @author andrewcutler
 */
public class AddEventFrame extends JFrame{

    public static String[] MONTHS = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    public static int[]    DAYSINREGYEAR = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    public static int[]   DAYSINLEAPYEAR = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    public static Integer[]   AVAILABLEYEARS = { 2007, 2008, 2009, 2010 };
    private JCheckBox checked;
    private JTextField nameField;
    private JTextArea descriptionArea;
    private DatePanel startComboPanel;
    private DatePanel endComboPanel;
    public AddEventFrame(){
        
        super("Add New Event");
        
        Container content = this.getContentPane();
        
        JLabel nameLabel = new JLabel("Name: ");
        nameLabel.setAlignmentX(Container.LEFT_ALIGNMENT);
        JLabel descriptionLabel = new JLabel("Description: ");
        descriptionLabel.setAlignmentX(Container.LEFT_ALIGNMENT);
        checked = new JCheckBox("Checkable?");
        
        nameField = new JTextField();
        descriptionArea = new JTextArea(10,30);
        descriptionArea.setEditable(true);
        JScrollPane descriptionPane = new JScrollPane(descriptionArea);
        
        JPanel namePanel = new JPanel();
        namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.LINE_AXIS));
        namePanel.add(nameLabel);
        namePanel.add(nameField);
        
        JPanel descPanel = new JPanel();
        descPanel.setLayout(new BoxLayout(descPanel, BoxLayout.PAGE_AXIS));
        descPanel.add(descriptionLabel);
        descPanel.add(descriptionPane);
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
        JButton submit = new JButton("Add Event");
        submit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                if(nameField.getText().equals(""))
                    return;
                Event event;
                if(checked.isSelected())
                    event = new UserCheckEvent();
                else
                    event = new UserEvent();
                
                event.setName(nameField.getText());
                event.setDescription(descriptionArea.getText());
                event.setStartDate(startComboPanel.getDate());
                event.setEndDate(endComboPanel.getDate());
                main.println("Adding User Event: " + event.getUniqueID());
                main.getUserCourse().addEvent(event);
                main.resetTabbedPane();
                clear();
                main.println("Added User Event: " + event.getUniqueID());
            }
            
        });
        
        JButton close = new JButton("Close Window");
        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt)
            {
                main.disposeEventFrame();
            }
        });
        buttonPanel.add(submit);
        buttonPanel.add(close);
        
        startComboPanel = new DatePanel("Start Date: ", main.getCurrentDate());
        endComboPanel   = new DatePanel("End Date: ", main.getCurrentDate(1));
        
        
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
        
        content.add(namePanel);
        content.add(descPanel);
        content.add(startComboPanel);
        content.add(endComboPanel);
        content.add(checked);
        content.add(buttonPanel);
        
        pack();
    }
    
    public void clear()
    {
        nameField.setText("");
        descriptionArea.setText("");
        if(checked.isSelected())
            checked.setSelected(false);
        
    }
    public void setVisible()
    {
        this.setVisible(true);
    }
    
    
    
    
    protected void update()
    {
        ((JPanel)this.getContentPane()).updateUI();
    }
    
    
    
    private class DatePanel extends JPanel
    {
        private JComboBox year;
        private JComboBox day;
        private JComboBox month;
        
        public DatePanel(String s, MyDate startDate)
        {
            super();
            month = new JComboBox(MONTHS);
            month.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {                
                int index = month.getSelectedIndex();
                System.out.println(index + "-" + month.getItemAt(index).toString());
                day.removeAllItems();
                for(int j = 0; j < AddEventFrame.DAYSINREGYEAR[index]; j++)
                {
                    day.addItem(new Integer(j+1));
                }
                
            }

            });
            day = new JComboBox();
            for(int j = 0; j < AddEventFrame.DAYSINREGYEAR[month.getSelectedIndex()]; j++)
                {
                    day.addItem(new Integer(j+1));
                }
            year = new JComboBox(AVAILABLEYEARS);
            
            month.setSelectedIndex(startDate.getMonth()-1);
            day.setSelectedIndex(startDate.getDayOfMonth());
            year.setSelectedItem(new Integer(startDate.getYear()));
            
            add(new JLabel(s));
            add(month);
            add(day);
            add(year);
        }
        
        public int getMonth()
        {
            return month.getSelectedIndex()+1;
        }
        public int getDay()
        {
            return ((Integer)day.getSelectedItem()).intValue();
        }
        public int getYear()
        {
            return ((Integer)year.getSelectedItem()).intValue();
        }
        
        public MyDate getDate()
        {
            return new MyDate(getDay(), getMonth(), getYear());
        }
    }
    
    
}
